// pages/application/application.js
import {more} from '../../utils/more'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    item:'get',
    data:[],
    mes:[]
  },
  /*点击按钮变色以及筛选框消失*/

  change1:function(){
    this.setData({
      index:0,
      item:'get'
    })
    var _this=this;
    wx.request({
      url: `https://api.yidan.work:2004/myReceive/${wx.getStorageSync('user_id')}`,
      method:'GET',
      header:{
        'content-type':'application/json'
      },
      success:function(res){
        console.log(res.data);
        _this.setData({
          data:res.data
        })
      },
      fail:function(res){
        console.log(res)
      }
    })
  },
  change2:function(){
    this.setData({
      index:1,
      item:'push'
    })
    var _this=this;
    wx.request({
      url: `https://api.yidan.work:2004/myApply/${wx.getStorageSync('user_id')}`,
      method:'GET',
      header:{
        'content-type':'application/json'
      },
      success:function(res){
        
        _this.setData({
          mes:res.data
        })
        for(var i=0;i<_this.data.mes.length;i++){
          var a = "mes["+i+"].apply_state";
          
          if(_this.data.mes[i].apply_state==1){
            _this.setData({
              [a]:'已同意'
            })
          }
          if(_this.data.mes[i].apply_state==2){
            _this.setData({
              [a]:'已拒绝'
            })
          }
          if(_this.data.mes[i].apply_state==0){
            _this.setData({
              [a]:'等待中'
            })
          }
          console.log(_this.data.mes[i].apply_state)
        }
        console.log(res.data)
        
      },
      fail:function(res){
        console.log(res)
      }
    })
  },
  list:function(){
    this.setData({
      show_list:!this.data.show_list
    })
  },
  ok:function(e){
    var num =e.currentTarget.dataset.id;
    var that = this;
    var user_id=wx.getStorageSync('user_id');
    wx.request({
      url: 'https://api.yidan.work:2004/agree',
      header:{'content-type':'application/JSON'},
      method: 'POST',
      data:{
        apply_id:that.data.data[num].apply_id,
        apply_post_id:that.data.data[num].apply_post_id
      },
      success:(res)=>{
        wx.showToast({
          title: '组队成功',
          duration:1000
        })
        console.log(res.data);
        getCurrentPages()[getCurrentPages().length - 1].onLoad()
      },
      fail:(res)=>{
        console.log(res.data)
      }
    })
  },


  no:function(e){
    var num =e.currentTarget.dataset.id;
    var that = this;
    var user_id=wx.getStorageSync('user_id');
    wx.request({
      url: 'https://api.yidan.work:2004/refuse',
      header:{'content-type':'application/JSON'},
      method: 'POST',
      data:{
        apply_id:that.data.data[num].apply_id,
        apply_post_id:that.data.data[num].apply_post_id
      },
      success:(res)=>{
        wx.showToast({
          title: '已拒绝',
          duration:1000
        })
        getCurrentPages()[getCurrentPages().length - 1].onLoad()
        console.log(res.data)
      },
      fail:(res)=>{
        console.log(res.data)
      }
    })
  },
  mores1:function(e){
    var that = this;
    var num =e.currentTarget.dataset.id;
    var id= that.data.data[num].user_id;
    more(id);
  },
  mores2:function(e){
    var that = this;
    var num =e.currentTarget.dataset.id;
    var id= that.data.mes[num].user_id;
    more(id);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      index:0,
      item:'get'
    })
    var _this=this;
    wx.request({
      url: `https://api.yidan.work:2004/myReceive/${wx.getStorageSync('user_id')}`,
      method:'GET',
      header:{
        'content-type':'application/json'
      },
      success:function(res){
        console.log(res.data);
        _this.setData({
          data:res.data
        })
      },
      fail:function(res){
        console.log(res)
      }
    })
  },

  onRefresh:function(){
    //导航条加载动画
    wx.showNavigationBarLoading()
    //loading 提示框
    wx.showLoading({
      title: 'Loading...',
    })
    console.log("下拉刷新啦");
    setTimeout(function () {
      wx.hideLoading();
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh:function(){
    this.onRefresh();
  },
})